package com.yeskery.nut.web.demo.config;

import com.yeskery.nut.annotation.bean.Bean;
import com.yeskery.nut.annotation.bean.Configuration;
import com.yeskery.nut.annotation.condition.ConditionalOnClass;
import com.yeskery.nut.annotation.condition.ConditionalOnProperty;
import com.yeskery.nut.extend.actuator.ActuatorPlugin;
import com.yeskery.nut.extend.datasource.hikaricp.HikaricpPlugin;
import com.yeskery.nut.extend.i18n.I18nPlugin;
import com.yeskery.nut.extend.jdbc.JdbcPlugin;
import com.yeskery.nut.extend.memory.MemoryPlugin;
import com.yeskery.nut.extend.mybatisplus.MyBatisPlusPlugin;
import com.yeskery.nut.extend.redis.JedisPlugin;

/**
 * 插件配置类
 * @author Yeskery
 * 2023/7/27
 */
@Configuration
public class PluginConfig {

    /**
     * HikariCP连接池插件
     * @param jdbcProperties
     * @return
     */
    @Bean
    public HikaricpPlugin hikaricpPlugin(JdbcProperties jdbcProperties) {
        System.out.println("username:" + jdbcProperties.getUsername());
        System.out.println("password:" + jdbcProperties.getPassword());
        return new HikaricpPlugin(jdbcProperties.getDriverClass(), jdbcProperties.getUrl(), jdbcProperties.getUsername(),
                jdbcProperties.getPassword());
    }

    @Bean
    public JdbcPlugin jdbcPlugin() {
        return new JdbcPlugin();
    }

    /**
     * MyBatis-Plus插件
     * @return
     */
    @Bean
    public MyBatisPlusPlugin myBatisPlusPlugin() {
        return new MyBatisPlusPlugin("com.yeskery.nut.web.demo.mapper");
    }

    @Bean
    public I18nPlugin i18nPlugin() {
        return new I18nPlugin("i18n");
    }

    /**
     * Jedis插件
     * @param redisProperties
     * @return
     */
    @Bean
    @ConditionalOnProperty(value = "redis.enable", havingValue = "true")
    @ConditionalOnClass(name = "redis.clients.jedis.Jedis")
    public JedisPlugin jedisPlugin(RedisProperties redisProperties) {
        return new JedisPlugin(redisProperties.getHost(), redisProperties.getPort(), redisProperties.getPassword(),
                redisProperties.getDatabase());
    }

    /**
     * 内存存储插件
     * @return
     */
    @Bean
    public MemoryPlugin memoryPlugin() {
        return new MemoryPlugin();
    }

    @Bean
    public ActuatorPlugin actuatorPlugin() {
        return new ActuatorPlugin();
    }
}
